﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace UpdateShare
{
    public class CmdHelper
    {
        /// <summary>
        /// 授权文件可执行权限
        /// </summary>
        /// <param name="fileName"></param>
        public static void PermissionX(string fileName)
        {
            var psiPermission = new ProcessStartInfo();
            psiPermission.FileName = "chmod";
            psiPermission.Arguments = "+x " + fileName;
            Process.Start(psiPermission).WaitForExit();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceName"></param>
        /// <param name="command">start/stop/restart/status</param>
        public static void SystemCtlService(string serviceName, string command)
        {
            var psi = new ProcessStartInfo();
            psi.FileName = "systemctl";
            psi.Arguments = command + " " + serviceName;
            Process.Start(psi).WaitForExit();
        }

        /// <summary>
        /// 启动应用程序，可等待
        /// </summary>
        /// <param name="serviceName"></param>
        public static async Task StartExe(string exeFile)
        {
            var psi = new ProcessStartInfo();
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && RuntimeInformation.OSArchitecture == Architecture.X64)
            {
                psi.FileName = exeFile+".exe";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                psi.FileName = exeFile;
            }

            Console.WriteLine("-- Running Application --");
            Console.WriteLine("Filename : " + psi.FileName);
            Console.WriteLine("Args : " + psi.Arguments);
           await Process.Start(psi).WaitForExitAsync(); 
        }
    }
}
